
<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 发布管理</el-breadcrumb-item>
                <el-breadcrumb-item>比赛管理</el-breadcrumb-item>
                <el-breadcrumb-item>报名列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
            <el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="index" width="55" align="center" label="排序"></el-table-column>
                <el-table-column prop="activityname" label="活动名称" width="180">
                </el-table-column>
                <el-table-column prop="realname" label="姓名" width="120">
                </el-table-column>
                <el-table-column prop="sex" label="性别" width="120" :formatter="formatter">
                </el-table-column>
                <el-table-column prop="userphone" label="手机号" width="120">
                </el-table-column>
                <el-table-column prop="useremail" label="邮箱地址" width="auto">
                </el-table-column>
                <el-table-column prop="applytime" label="报名时间"  width="160">
                </el-table-column>
                <el-table-column prop="paymoney" label="实付金额"  width="90">
                </el-table-column>
                <el-table-column prop="activitymoney" label="应付金额"  width="90">
                </el-table-column>
                <el-table-column prop="paystate" label="缴费状态"  width="150" align="center">
                    <template slot-scope="scope">
                        <template v-if="scope.row.paystate==2">
                            <el-button type="danger" icon="el-icon-close" round>未支付</el-button>
                        </template>
                        <template v-else-if="scope.row.paystate==0">
                            <el-button type="warning" icon="el-icon-loading" round>待支付</el-button>
                        </template>
                        <template v-else-if="scope.row.paystate==3">
                            <el-button type="danger" icon="el-icon-refresh-right" round>申请退款</el-button>
                        </template>
                        <template v-else>
                            <el-button type="success" icon="el-icon-check" round>已支付</el-button>
                        </template>
                    </template>
                    <!-- 缴费成功 -->
                    <!-- <el-button type="success" icon="el-icon-check" circle></el-button> -->
                    <!-- 缴费失败 -->
                    <!-- <el-button type="danger" icon="el-icon-close" circle></el-button> -->
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="50px">
                <el-form-item label="日期">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.date" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchData } from '../api/index';
    import { test } from '../api/index';
    export default {
        name: 'basetable',
        data() {
            return {
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {
                    name: '',
                    date: '',
                    address: ''
                },
                idx: -1,
                id: -1
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    console.log('d.realname :', d.realname);
                    if (d.realname.indexOf(this.select_word) > -1 ||
                        d.activityname.indexOf(this.select_word) > -1) 
                    {
                        return d;
                    }
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                this.$axios.get('/api/Activity/applyList').then((result) => {
                    this.tableData = result.data;
                }).catch((err) => {
                    console.log('err :', err);
                });
            },
            search() {
                this.is_search = true;
            },
            formatter(row, column) {
                return row.sex==0? '女' : '男';
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.idx = index;
                this.id = row.id;
                this.form = {
                    id: row.id,
                    name: row.name,
                    date: row.date,
                    address: row.address
                }
                this.editVisible = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.id = row.id;
                this.delVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx+1} 行成功`);
                if(this.tableData[this.idx].id === this.id){
                    this.$set(this.tableData, this.idx, this.form);
                }else{
                    for(let i = 0; i < this.tableData.length; i++){
                        if(this.tableData[i].id === this.id){
                            this.$set(this.tableData, i, this.form);
                            return ;
                        }
                    }
                }
            },
            // 确定删除
            deleteRow(){
                this.$message.success('删除成功');
                this.delVisible = false;
                if(this.tableData[this.idx].id === this.id){
                    this.tableData.splice(this.idx, 1);
                }else{
                    for(let i = 0; i < this.tableData.length; i++){
                        if(this.tableData[i].id === this.id){
                            this.tableData.splice(i, 1);
                            return ;
                        }
                    }
                }
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
</style>
